package com.example.hanfu_backend.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.hanfu_backend.entity.HanfuShop;
import com.example.hanfu_backend.entity.HanfuType;
import com.example.hanfu_backend.mapper.HanfuShopMapper;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/hanfuShop")
public class HanfuShopController {
    @Autowired
    HanfuShopMapper hanfuShopMapper;

    @GetMapping("/getAllShops")
    @Operation(summary = "不分页查询所有店铺")
    public List<HanfuShop> getAllShops() {
        return hanfuShopMapper.selectList(null);
    }

    @GetMapping("/getShops")
    @Operation(summary = "分页查询所有店铺")
    public Map<String,Object> getShops(@RequestParam("pageNum")Integer pageNum,@RequestParam("pageSize")Integer pageSize) {
        Page<HanfuShop> page = new Page<>(pageNum,pageSize); // 创建分页对象
        // 设置分页查询条件
        QueryWrapper<HanfuShop> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("hanfushop_id");
        IPage<HanfuShop> iPage = hanfuShopMapper.selectPage(page,queryWrapper); // 执行分页查询操作
        // 封装分页查询结果
        Map<String,Object> map = new HashMap<>();
        map.put("total",iPage.getTotal());
        map.put("records",iPage.getRecords());
        return map;
    }

    @GetMapping("/getShopById")
    @Operation(summary = "通过店铺ID分页查询店铺")
    public Map<String,Object> queryShopById(@RequestParam("pageNum")Integer pageNum,@RequestParam("pageSize")Integer pageSize,@RequestParam("hanfuShopId")Integer hanfuShopId) {
        Page<HanfuShop> page = new Page<>(pageNum,pageSize);
        QueryWrapper<HanfuShop> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("hanfushop_id",hanfuShopId);
        IPage<HanfuShop> iPage = hanfuShopMapper.selectPage(page,queryWrapper);
        Map<String,Object> map = new HashMap<>();
        map.put("total",iPage.getTotal());
        map.put("records",iPage.getRecords());
        return map;
    }

    @GetMapping("/getShopByName")
    @Operation(summary = "通过店铺名称分页模糊查询店铺")
    public Map<String,Object> queryShopByName(@RequestParam("pageNum")int pageNum,@RequestParam("pageSize")Integer pageSize,@RequestParam("hanfuShopName")String hanfuShopName) {
        Page<HanfuShop> page = new Page<>(pageNum,pageSize);
        QueryWrapper<HanfuShop> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("hanfushop_name",hanfuShopName);
        IPage<HanfuShop> iPage = hanfuShopMapper.selectPage(page,queryWrapper);
        Map<String,Object> map = new HashMap<>();
        map.put("total",iPage.getTotal());
        map.put("records",iPage.getRecords());
        return map;
    }

    @DeleteMapping("/deleteShop")
    @Operation(summary = "根据ID删除某一店铺")
    public int deleteShop(@RequestParam("hanfuShopId")Integer hanfuShopId) {
        return hanfuShopMapper.deleteById(hanfuShopId);
    }

    @PostMapping("/insertShop")
    @Operation(summary = "添加店铺")
    public int insertShop(@RequestBody HanfuShop hanfuShop) {
        return hanfuShopMapper.insert(hanfuShop);
    }

    @PutMapping("/updateShop")
    @Operation(summary = "根据Id修改某一店铺")
    public int updateShop(@RequestBody HanfuShop hanfuShop) {
        return hanfuShopMapper.updateById(hanfuShop);
    }
}
